package com.example.myapplication0523.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication0523.R;
import com.example.myapplication0523.bean.HomeBean;

import java.util.List;

public class AliHomeAdapter extends BaseAdapter {

    private final Context context;
    private List<HomeBean> list;

    public AliHomeAdapter(Context context, List<HomeBean> list) {
        this.context= context;
        this.list= list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder= null;
        if(convertView== null){
            holder= new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_home, parent, false);
            holder.iconIv= convertView.findViewById(R.id.item_icon);
            holder.nameTv= convertView.findViewById(R.id.item_name);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        holder.iconIv.setImageResource(list.get(position).getIcon());
        holder.nameTv.setText(list.get(position).getName());
        return convertView;
    }

    class ViewHolder{
        ImageView iconIv;
        TextView nameTv;
    }

    public void setNewList(List<HomeBean> list){
        this.list= list;
        notifyDataSetChanged();
    }

}
